package cn.edu.nankai.bookstore.dao.impi;


import cn.edu.nankai.bookstore.dao.CustomerDao;
import cn.edu.nankai.bookstore.domain.po.Customer;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;

public class CustomerDaoImpl extends GenericBaseDao implements CustomerDao {

    @Override
    public int insert(Customer customer) {
        String sql = "insert into customer values(null,?,?,?,?)";
        int res = 0;
        try {
            this.getConnection();
            this.executeInsert(sql,customer.getName(),
                    customer.getTelephone(),
                    customer.getAddress(),
                    customer.getPwd());
            res = result;
            this.closeAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public int delete(Integer id) {
        String sql = "delete from customer where customer_id = ?";
        int res = 0;
        try {
            this.getConnection();
            this.executeUpdate(sql,id);
            res = result;
            this.closeAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public int delete(Customer customer) {
        return delete(customer.getCustomerId());
    }


    @Override
    public int update(Customer customer) {
        String sql = "update customer set name = ?,telephone = ?,address = ?," +
                "pwd = ? where customer_id = ?";
        int res = 0;
        try {
            this.getConnection();
            this.executeUpdate(sql,customer.getName(),
                    customer.getTelephone(),
                    customer.getAddress(),
                    customer.getPwd(),
                    customer.getCustomerId());
            res = result;
            this.closeAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public ArrayList<Customer> findAll() {
        ArrayList<Customer> customers = new ArrayList<>();
        String sql = "select * from customer";
        try {
            this.getConnection();
            this.executeQuery(sql);
            while (rs.next()){
                customers.add(new Customer(rs.getInt("customer_id"),
                        rs.getString("name"),
                        rs.getBigDecimal("telephone"),
                        rs.getString("address"),
                        rs.getString("pwd")));
            }
            this.closeAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return customers;
    }


    @Override
    public Customer findById(Integer id) {
        String sql = "select * from customer where customer_id = ?";
        Customer customer = null;
        try {
            this.getConnection();
            this.executeQuery(sql,id);
            while(rs.next()){
                customer = new Customer(rs.getInt("customer_id"),
                        rs.getString("name"),
                        rs.getBigDecimal("telephone"),
                        rs.getString("address"),
                        rs.getString("pwd"));
            }
            this.closeAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return customer;
    }


    @Override
    public List<Customer> findBySql(String sql, Object... params) {
        List<Customer> customers = null;
        try {
            this.getConnection();
            this.executeQuery(sql,params);
            if(rs!=null){
                customers = new ArrayList<>();
                while(rs.next()){
                    Customer customer = new Customer(rs.getInt("customer_id"),
                            rs.getString("name"),
                            rs.getBigDecimal("telephone"),
                            rs.getString("address"),
                            rs.getString("pwd"));
                    customers.add(customer);
                }
            }
            this.closeAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return customers;
    }

    @Override
    public Customer findByTel(BigDecimal telephone) {
        String sql = "select * from customer where telephone = ?";
        Customer customer = null;
        try {
            this.getConnection();
            this.executeQuery(sql,telephone);
            while(rs.next()){
                customer = new Customer(rs.getInt("customer_id"),
                        rs.getString("name"),
                        rs.getBigDecimal("telephone"),
                        rs.getString("address"),
                        rs.getString("pwd"));
            }
            this.closeAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return customer;
    }


}
